// const { name } = require('./package')
const path = require('path')

const resolve = (dir) => {
    return path.join(__dirname, dir)
}
// gzip压缩
const CompressionWebpackPlugin = require('compression-webpack-plugin')

const config = {
    devServer: {
        port: 3000,
        // proxy: {
        // "/api": {
        //     target: "",
        //     ws: true,
        //     secure: false,
        //     changeOrigin: true,
        // },
        // },
        disableHostCheck: true,
        // headers: { 'Access-Control-Allow-Origin': '*' },
        // before(app) {
        //     app.get(/.*.(js)$/, (req, res, next) => {
        //         req.url = req.url + '.gz'
        //         res.set('Content-Encoding', 'gzip')
        //         next()
        //     })
        // },
    },
    productionSourceMap: false,
    publicPath: '/',
    chainWebpack: (config) => {
        config.resolve.alias.set('@', resolve('src'))
        if (process.env.NODE_ENV === 'development') {
            config.module
                .rule('thejs')
                .test(/\.js$/)
                .include.add(path.resolve('src'))
                .add(path.resolve('node_modules/element-ui/packages'))
                .end()
                .use('babel-loader')
                .loader('babel-loader')
                .end()
        } else {
            // 移除 prefetch 插件
            config.plugins.delete('prefetch')
            // 移除 preload 插件
            config.plugins.delete('preload')
            // 查看资源树
            config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin).end()
        }
    },
    configureWebpack: (config) => {
        if (process.env.NODE_ENV === 'production') {
            // config.externals = cdn.externals
            config.plugins.push(
                new CompressionWebpackPlugin({
                    filename: (info) => {
                        return `${info.path}.gz${info.query}`
                    },
                    algorithm: 'gzip',
                    threshold: 10240, // 只有大小大于该值的资源会被处理 10240
                    test: new RegExp('\\.(' + ['js'].join('|') + ')$'),
                    minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
                    deleteOriginalAssets: false, // 删除原文件
                })
            )
        }
    },
    lintOnSave: process.env.NODE_ENV === 'development',
}

module.exports = config
